from datetime import timedelta

import pytz
from django.conf import settings
from django.utils import timezone
from rest_framework.decorators import action
from rest_framework.generics import ListAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from goods.models import GoodsVisitCount
from users.models import User


class HomeView(ViewSet):
    # permission_classes = [IsAdminUser]
    @action(methods=['get'],detail=False)
    def total_count(self,request):
        count= User.objects.all().count()
        date_now = timezone.now()
        timeobj = pytz.timezone(settings.TIME_ZONE)
        date = date_now.astimezone(timeobj).date()
        return Response({
            "count":count,
            "date":date
        })
    @action(methods=["get"],detail=False)
    def day_increment(self,request):
        permission_classes = [IsAdminUser]

        date=timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE))
        date_0= date.replace(hour=0,minute=0,second=0)
        count= User.objects.filter(date_joined__gte=date_0).count()
        return Response({
            "count":count,
            "date":date.date()
        })
    @action(methods=['get'],detail=False)
    def day_active(self,request):
        date = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE))
        date_0 = date.replace(hour=0, minute=0, second=0)
        count = User.objects.filter(last_login__gte=date_0).count()
        return Response({
            "count": count,
            "date": date.date()})
    @action(methods=['get'],detail=False)
    def day_orders(self,request):
        date = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE))
        date_0 = date.replace(hour=0, minute=0, second=0)
        users = User.objects.filter(orderinfo__create_time__gte=date_0)
        count = len(set(users))
        return Response({
            "count": count,
            "date": date.date()})
    # TODO 时间运算计算，时区对象获取，时区转换
    @action(methods=['get'],detail=False)
    def month_increment(self,request):
        content=[]
        date = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE))
        date_0 = date.replace(hour=0, minute=0, second=0)-timedelta(days=29)
        for i in range(30):
            day_start = date_0+timedelta(days=i)
            day_end  = day_start+ timedelta(days=1)
            count = User.objects.filter(date_joined__gte=day_start, date_joined__lt=day_end).count()
            content.append({
                "count":count,
                "date":day_start
            })
        return Response(content

        )
    @action(methods=['get'],detail=False)
    def goods_day_views(self,request):
        content = []
        date = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE))
        date_0 = date.replace(hour=0, minute=0, second=0)
        data= GoodsVisitCount.objects.filter(create_time__gte=date_0)
        for i in data:
            content.append({
                'count':i.count,
                'category':i.category.name
            })
        return Response(content)
# 序列化返回GoodsVisitCount多条数据
# class GoodsVisitCountView(ListAPIView):
#     queryset = GoodsVisitCount.objects.all()
#     serializer_class = GoodsVisitCountSerializer
#
#     permission_classes = [IsAdminUser]
#
#     def get_queryset(self):
#         cur_0_time = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE)). \
#             replace(hour=0, minute=0, second=0, microsecond=0)
#         return self.queryset.filter(create_time__gte=cur_0_time)